package com.voicechat.firstmet.fragment;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.adapter.EmojiGvAdapter;
import com.voicechat.firstmet.adapter.EmotionPagerAdapter;
import com.voicechat.firstmet.adapter.ViewPagerAdapter;
import com.voicechat.firstmet.base.BaseFragment;
import com.voicechat.firstmet.bean.EmojiEntity;
import com.voicechat.firstmet.utils.DisplayUtil;
import com.voicechat.firstmet.utils.FileUtils;
import com.voicechat.firstmet.utils.JsonParseUtil;
import com.voicechat.firstmet.utils.Utils;
import com.voicechat.firstmet.view.CirclePageIndicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.fragment_shar_girls
 * @ClassName: EmojiShowFragment
 * @Description: 表情布局类
 * @Author: ljp
 * @CreateDate: 2018/10/19 9:32
 * @Version: 1.0
 */
public class EmojiShowFragment extends BaseFragment {
    @BindView(R.id.vp_emoji)
    ViewPager vpEmoji;
    Unbinder unbinder;
    private static EditText editMsg;
    @BindView(R.id.ll_point_group)
    CirclePageIndicator llPointGroup;
    private EmotionPagerAdapter emotionPagerGvAdapter;
    private List<EmojiEntity> emojiEntities;
    private EmojiGvAdapter adapter;
    private String TAG = "EmojiShowFragment";
    private ViewPagerAdapter mAdapter;
    private ArrayList<GridView> gridViews;
    public static int gridColumn = 7;//grid的列数
    public static int gridNum = 21;//grid的一页总数
    private int screenWidth;
    private int spacing;
    private int itemWidth;
    private int gvHeight;

    @Override
    protected int loadLayout() {
        return R.layout.fragment_emojishow;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        unbinder = ButterKnife.bind(this, view);
        initView();
        initDatas();
    }
    public static EmojiShowFragment newInstance() {
        //Bundle args = new Bundle();
        EmojiShowFragment fragment = new EmojiShowFragment();
        //editMsg = editText;
        //fragment_shar_girls.setArguments(args);
        return fragment;
    }

    public static void setEditMsg(EditText editMsg) {
        EmojiShowFragment.editMsg = editMsg;
    }

    /**
     * @return
     * @method
     * @description 数据控件实例化
     * @date: 2018/11/13 10:07
     * @author: ljp
     */
    private void initView() {
        // 获取屏幕宽度
        screenWidth = Utils.getSrecchWidth(getActivity());
        // item的间距
        spacing = DisplayUtil.dp2px(getActivity(), 12);
        // 动态计算item的宽度和高度
        itemWidth = (screenWidth - spacing ) / 7;
        //动态计算gridview的总高度
        gvHeight = itemWidth * 3 + spacing * 6;
        gridViews = new ArrayList<>();
        emojiEntities = JsonParseUtil.parseEmojiList(FileUtils.readAssetsFile(context, "EmojiList.json"));
        mAdapter = new ViewPagerAdapter();
        vpEmoji.setAdapter(mAdapter);
        llPointGroup.setVisibility(View.VISIBLE);
        llPointGroup.setViewPager(vpEmoji);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(screenWidth, gvHeight);
        vpEmoji.setLayoutParams(params);


    }
    private void initDatas() {

        //计算viewpager一共显示几页
        int pageSize = emojiEntities.size() % gridNum == 0
                ? emojiEntities.size() / gridNum
                : emojiEntities.size() / gridNum + 1;
        for (int i = 0; i < pageSize; i++) {
            GridView gridView = new GridView(activity);
            //设置7列
            gridView.setNumColumns(7);
            gridView.setPadding(spacing, spacing, spacing, spacing);
            gridView.setHorizontalSpacing(spacing);
            gridView.setVerticalSpacing(spacing * 2);
            //设置GridView的宽高
            //ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(screenWidth, gvHeight);
           // gridView.setLayoutParams(params);
            EmojiGvAdapter adapter = new EmojiGvAdapter(context,emojiEntities, i,itemWidth);
            gridView.setAdapter(adapter);
            int finalI = i;
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    int selectionStart = editMsg.getSelectionStart();
                    Editable editable = editMsg.getText();
                    if (adapter.getItemName(position).equals("del")) {
                        //表示点击的是删除按钮
                        KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL);
                        editMsg.onKeyDown(KeyEvent.KEYCODE_DEL, event);
                    } else {
                        Log.i(TAG, "onClick: ------"+adapter.getItem(position)+"---"+position);
                        String item = adapter.getItem(position);
                        editable.insert(selectionStart,item);
                        //editMsg.append(item);
                    }
                }
            });
            gridViews.add(gridView);
        }
        mAdapter.add(gridViews);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
